﻿cmake_minimum_required(VERSION 3.1)

project(wotermall)


SET(CMAKE_EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
SET(CMAKE_EXECUTABLE_OUTPUT_PATH_DEBUG ${PROJECT_SOURCE_DIR}/bin)
SET(CMAKE_EXECUTABLE_OUTPUT_PATH_RELEASE ${PROJECT_SOURCE_DIR}/bin)
SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/bin)
SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG ${PROJECT_SOURCE_DIR}/bin)
SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE ${PROJECT_SOURCE_DIR}/bin)
SET(CMAKE_LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
SET(CMAKE_LIBRARY_OUTPUT_PATH_DEBUG ${PROJECT_SOURCE_DIR}/bin)
SET(CMAKE_LIBRARY_OUTPUT_PATH_RELEASE ${PROJECT_SOURCE_DIR}/bin)
SET(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/bin)
SET(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_DEBUG ${PROJECT_SOURCE_DIR}/bin)
SET(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_RELEASE ${PROJECT_SOURCE_DIR}/bin)

option(QML_MODE "develop mobile version on desktop." false)

if(QML_MODE)
    add_definitions(-DQML_MODE)
endif()

# build qterm item to a static or shared libarary

set(KXVER_ROOT_DIR ${PROJECT_SOURCE_DIR}/kxver)
set(KXTERM_ROOT_DIR ${PROJECT_SOURCE_DIR}/kxterm)
set(KXVNC_ROOT_DIR ${PROJECT_SOURCE_DIR}/kxvnc)
set(KXFTP_ROOT_DIR ${PROJECT_SOURCE_DIR}/kxftp)
set(KXUTIL_ROOT_DIR ${PROJECT_SOURCE_DIR}/kxutil)


if(WIN32)
    set(CMAKE_DEBUG_POSTFIX "d")

    message("------begin to confirm the SDK version through rc.exe-----")
    execute_process(COMMAND "where" "rc.exe")
    message("------end to confirm the SDK version through rc.exe-----")

    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /utf-8")
    set(OPENSSL_ROOT_DIR ${PROJECT_SOURCE_DIR}/third/openssl/winx)
    set(ZLIB_ROOT_DIR ${PROJECT_SOURCE_DIR}/third/zlib/winx)
    set(LIBSSH_ROOT_DIR ${PROJECT_SOURCE_DIR}/third/libssh/winx)
    set(LIBQSSH_ROOT_DIR ${PROJECT_SOURCE_DIR}/third/qssh/winx)
    set(FREERDP_ROOT_DIR ${PROJECT_SOURCE_DIR}/third/freerdp/winx)
    set(LIBOPENH264_ROOT_DIR ${PROJECT_SOURCE_DIR}/third/openh264/winx)
    set(LIBPORTAUDIO_ROOT_DIR ${PROJECT_SOURCE_DIR}/third/portaudio/winx)
    set(LIBOPUS_ROOT_DIR ${PROJECT_SOURCE_DIR}/third/opus/winx)
    set(LIBJPEG_ROOT_DIR ${PROJECT_SOURCE_DIR}/third/jpeg/winx)
    set(LIBYUV_ROOT_DIR ${PROJECT_SOURCE_DIR}/third/libyuv/winx)
    set(LIBTELNET_ROOT_DIR ${PROJECT_SOURCE_DIR}/third/libtelnet/winx)
    set(LIBMODEM_ROOT_DIR ${PROJECT_SOURCE_DIR}/third/modem/winx)
    set(LIBSQLITE_ROOT_DIR ${PROJECT_SOURCE_DIR}/third/sqlite/winx)
    set(LIBWINPTY_ROOT_DIR ${PROJECT_SOURCE_DIR}/third/winpty/winx)
    set(LIBPTYQT_ROOT_DIR ${PROJECT_SOURCE_DIR}/third/ptyqt/winx)

    file(GLOB MY_COPY_FILES ${MY_COPY_FILES} ${LIBWINPTY_ROOT_DIR}/bin/winpty.dll)
    file(GLOB MY_COPY_FILES ${MY_COPY_FILES} ${LIBWINPTY_ROOT_DIR}/bin/winpty-agent.exe)
    file(GLOB MY_COPY_FILES ${MY_COPY_FILES} ${LIBSSH_ROOT_DIR}/bin/*.dll)
    file(GLOB MY_COPY_FILES ${MY_COPY_FILES} ${FREERDP_ROOT_DIR}/bin/*.dll)
    file(GLOB MY_COPY_FILES ${MY_COPY_FILES} ${OPENSSL_ROOT_DIR}/bin/*.dll)
    file(GLOB MY_COPY_FILES ${MY_COPY_FILES} ${LIBPTYQT_ROOT_DIR}/bin/*.dll)
    file(GLOB MY_COPY_FILES ${MY_COPY_FILES} ${LIBPTYQT_ROOT_DIR}/bin/*.exe)
elseif(ANDROID)
    if(NOT ${ANDROID_NATIVE_API_LEVEL} STREQUAL "21")
        message("------------------ abi and api check. -----------------------------")
        message("android native api level must be set to 21.")
    endif()
    message("---------------------android ABI:${ANDROID_ABI} API:${ANDROID_NATIVE_API_LEVEL}---------------")
    if(${ANDROID_ABI} STREQUAL "x86")
        set(OPENSSL_ROOT_DIR ${PROJECT_SOURCE_DIR}/third/openssl/android/x86)
        set(ZLIB_ROOT_DIR ${PROJECT_SOURCE_DIR}/third/zlib/android/x86)
        set(LIBSSH_ROOT_DIR ${PROJECT_SOURCE_DIR}/third/libssh/android/x86)
        set(LIBQSSH_ROOT_DIR ${PROJECT_SOURCE_DIR}/third/qssh/android/x86)
        set(FREERDP_ROOT_DIR ${PROJECT_SOURCE_DIR}/third/freerdp/android/x86)
        set(LIBOPENH264_ROOT_DIR ${PROJECT_SOURCE_DIR}/third/openh264/android/x86)
        set(LIBPORTAUDIO_ROOT_DIR ${PROJECT_SOURCE_DIR}/third/portaudio/android/x86)
        set(LIBOPUS_ROOT_DIR ${PROJECT_SOURCE_DIR}/third/opus/android/x86)
        set(LIBJPEG_ROOT_DIR ${PROJECT_SOURCE_DIR}/third/jpeg/android/x86)
        set(LIBYUV_ROOT_DIR ${PROJECT_SOURCE_DIR}/third/libyuv/android/x86)
        set(LIBTELNET_ROOT_DIR ${PROJECT_SOURCE_DIR}/third/libtelnet/android/x86)
        set(LIBMODEM_ROOT_DIR ${PROJECT_SOURCE_DIR}/third/modem/android/x86)
        set(LIBSQLITE_ROOT_DIR ${PROJECT_SOURCE_DIR}/third/sqlite/android/x86)
        set(LIBWINPTY_ROOT_DIR ${PROJECT_SOURCE_DIR}/third/winpty/android/x86)
        set(LIBPTYQT_ROOT_DIR ${PROJECT_SOURCE_DIR}/third/ptyqt/android/x86)
    elseif(${ANDROID_ABI} STREQUAL "armeabi-v7a")
        set(OPENSSL_ROOT_DIR ${PROJECT_SOURCE_DIR}/third/openssl/android/arm)
        set(ZLIB_ROOT_DIR ${PROJECT_SOURCE_DIR}/third/zlib/android/arm)
        set(LIBSSH_ROOT_DIR ${PROJECT_SOURCE_DIR}/third/libssh/android/arm)
        set(LIBQSSH_ROOT_DIR ${PROJECT_SOURCE_DIR}/third/qssh/android/arm)
        set(FREERDP_ROOT_DIR ${PROJECT_SOURCE_DIR}/third/freerdp/android/arm)
        set(LIBOPENH264_ROOT_DIR ${PROJECT_SOURCE_DIR}/third/openh264/android/arm)
        set(LIBPORTAUDIO_ROOT_DIR ${PROJECT_SOURCE_DIR}/third/portaudio/android/arm)
        set(LIBOPUS_ROOT_DIR ${PROJECT_SOURCE_DIR}/third/opus/android/arm)
        set(LIBJPEG_ROOT_DIR ${PROJECT_SOURCE_DIR}/third/jpeg/android/arm)
        set(LIBYUV_ROOT_DIR ${PROJECT_SOURCE_DIR}/third/libyuv/android/arm)
        set(LIBTELNET_ROOT_DIR ${PROJECT_SOURCE_DIR}/third/libtelnet/android/arm)
        set(LIBMODEM_ROOT_DIR ${PROJECT_SOURCE_DIR}/third/modem/android/arm)
        set(LIBSQLITE_ROOT_DIR ${PROJECT_SOURCE_DIR}/third/sqlite/android/arm)
        set(LIBWINPTY_ROOT_DIR ${PROJECT_SOURCE_DIR}/third/winpty/android/arm)
        set(LIBPTYQT_ROOT_DIR ${PROJECT_SOURCE_DIR}/third/ptyqt/android/arm)
    elseif(${ANDROID_ABI} STREQUAL "arm64-v8a")
        set(OPENSSL_ROOT_DIR ${PROJECT_SOURCE_DIR}/third/openssl/android/arm64)
        set(ZLIB_ROOT_DIR ${PROJECT_SOURCE_DIR}/third/zlib/android/arm64)
        set(LIBSSH_ROOT_DIR ${PROJECT_SOURCE_DIR}/third/libssh/android/arm64)
        set(LIBQSSH_ROOT_DIR ${PROJECT_SOURCE_DIR}/third/qssh/android/arm64)
        set(FREERDP_ROOT_DIR ${PROJECT_SOURCE_DIR}/third/freerdp/android/arm64)
        set(LIBOPENH264_ROOT_DIR ${PROJECT_SOURCE_DIR}/third/openh264/android/arm64)
        set(LIBPORTAUDIO_ROOT_DIR ${PROJECT_SOURCE_DIR}/third/portaudio/android/arm64)
        set(LIBOPUS_ROOT_DIR ${PROJECT_SOURCE_DIR}/third/opus/android/arm64)
        set(LIBJPEG_ROOT_DIR ${PROJECT_SOURCE_DIR}/third/jpeg/android/arm64)
        set(LIBYUV_ROOT_DIR ${PROJECT_SOURCE_DIR}/third/libyuv/android/arm64)
        set(LIBTELNET_ROOT_DIR ${PROJECT_SOURCE_DIR}/third/libtelnet/android/arm64)
        set(LIBMODEM_ROOT_DIR ${PROJECT_SOURCE_DIR}/third/modem/android/arm64)
        set(LIBSQLITE_ROOT_DIR ${PROJECT_SOURCE_DIR}/third/sqlite/android/arm64)
        set(LIBWINPTY_ROOT_DIR ${PROJECT_SOURCE_DIR}/third/winpty/android/arm64)
        set(LIBPTYQT_ROOT_DIR ${PROJECT_SOURCE_DIR}/third/ptyqt/android/arm64)
    endif()

    file(GLOB MY_COPY_FILES ${LIBSSH_ROOT_DIR}/lib/libssh.*)
    file(GLOB MY_COPY_FILES ${MY_COPY_FILES} ${FREERDP_ROOT_DIR}/lib/librdp.*)
    file(GLOB MY_COPY_FILES ${MY_COPY_FILES} ${OPENSSL_ROOT_DIR}/lib/lib*.so*)
    file(GLOB MY_COPY_FILES ${MY_COPY_FILES} ${FREERDP_ROOT_DIR}/lib/lib*.so*)
    file(GLOB MY_COPY_FILES ${MY_COPY_FILES} ${LIBOPENH264_ROOT_DIR}/lib/lib*.so*)
    file(GLOB MY_COPY_FILES ${MY_COPY_FILES} ${LIBPORTAUDIO_ROOT_DIR}/lib/lib*.so*)
    file(GLOB MY_COPY_FILES ${MY_COPY_FILES} ${LIBOPUS_ROOT_DIR}/lib/lib*.so*)
    file(GLOB MY_COPY_FILES ${MY_COPY_FILES} ${LIBJPEG_ROOT_DIR}/lib/lib*.so*)
    file(GLOB MY_COPY_FILES ${MY_COPY_FILES} ${LIBYUV_ROOT_DIR}/lib/lib*.so*)
    file(GLOB MY_COPY_FILES ${MY_COPY_FILES} ${LIBPTYQT_ROOT_DIR}/lib/lib*.so*)
else()
    set(OPENSSL_ROOT_DIR ${PROJECT_SOURCE_DIR}/third/openssl/unix)
    set(ZLIB_ROOT_DIR ${PROJECT_SOURCE_DIR}/third/zlib/unix)
    set(LIBSSH_ROOT_DIR ${PROJECT_SOURCE_DIR}/third/libssh/unix)
    set(LIBQSSH_ROOT_DIR ${PROJECT_SOURCE_DIR}/third/qssh/unix)
    set(FREERDP_ROOT_DIR ${PROJECT_SOURCE_DIR}/third/freerdp/unix)
    set(LIBOPENH264_ROOT_DIR ${PROJECT_SOURCE_DIR}/third/openh264/unix)
    set(LIBPORTAUDIO_ROOT_DIR ${PROJECT_SOURCE_DIR}/third/portaudio/unix)
    set(LIBOPUS_ROOT_DIR ${PROJECT_SOURCE_DIR}/third/opus/unix)
    set(LIBJPEG_ROOT_DIR ${PROJECT_SOURCE_DIR}/third/jpeg/unix)
    set(LIBYUV_ROOT_DIR ${PROJECT_SOURCE_DIR}/third/libyuv/unix)
    set(LIBTELNET_ROOT_DIR ${PROJECT_SOURCE_DIR}/third/libtelnet/unix)
    set(LIBMODEM_ROOT_DIR ${PROJECT_SOURCE_DIR}/third/modem/unix)
    set(LIBSQLITE_ROOT_DIR ${PROJECT_SOURCE_DIR}/third/sqlite/unix)
    set(LIBWINPTY_ROOT_DIR ${PROJECT_SOURCE_DIR}/third/winpty/unix)
    set(LIBPTYQT_ROOT_DIR ${PROJECT_SOURCE_DIR}/third/ptyqt/unix)

    file(GLOB MY_COPY_FILES ${LIBSSH_ROOT_DIR}/lib/libssh.*)
    file(GLOB MY_COPY_FILES ${MY_COPY_FILES} ${FREERDP_ROOT_DIR}/lib/librdp.*)
    file(GLOB MY_COPY_FILES ${MY_COPY_FILES} ${OPENSSL_ROOT_DIR}/lib/lib*.dylib)
    file(GLOB MY_COPY_FILES ${MY_COPY_FILES} ${OPENSSL_ROOT_DIR}/lib/lib*.so*)
    file(GLOB MY_COPY_FILES ${MY_COPY_FILES} ${LIBPTYQT_ROOT_DIR}/bin/lib*.so*)
    file(GLOB MY_COPY_FILES ${MY_COPY_FILES} ${LIBPTYQT_ROOT_DIR}/bin/lib*.dylib)
endif()

if(ANDROID)
    file(COPY ${MY_COPY_FILES} DESTINATION ${PROJECT_SOURCE_DIR}/bin)
    set(MYPATH "")
    foreach(MYFILE ${MY_COPY_FILES})
        if(NOT (${MYFILE} MATCHES ".so."))
            string(JOIN "," var ${MYPATH} ${MYFILE})
            set(MYPATH ${var})
        endif()
    endforeach()
    set(ANDROID_EXTRA_LIBS ${MYPATH} CACHE INTERNAL "")
    message("ANDROID_EXTRA_LIBS:${ANDROID_EXTRA_LIBS}")
    get_filename_component(QT_TOOL_PATH ${QT_QMAKE_EXECUTABLE}/../../ ABSOLUTE)
    message("QT_TOOL_PATH:${QT_TOOL_PATH}")
else()
    file(COPY ${MY_COPY_FILES} DESTINATION ${PROJECT_SOURCE_DIR}/bin)
    message("copy file list:${MY_COPY_FILES}")
endif()

message("root path:${PROJECT_SOURCE_DIR}")

add_subdirectory(kxver)
add_subdirectory(kxterm)
add_subdirectory(kxvnc)
add_subdirectory(kxutil)
add_subdirectory(kxftp)
add_subdirectory(woterm)



